//Rajouter
var refresh = null;
var savedtime;
////////////////////////////////

function completerlisteFOP(data,id_div){
	var html = "";
	html +="<tr>";
	$.each(data.content, function(key,val){
		$.each(val, function(key,val){

			html+="<td>"+val+"</td>";
		});
		html+="</tr><tr>";
	});
	$(id_div).html(html);
}

function completercurrentFOP(data,id_div){	
	var line =0;
	var html = "";
	var thisone = false;
	var current = parseInt(data.current_line);
	if (current == 0){
		html+="<tr style='color:red;background:#DCDCDC;' class='current_line' id='current_line'><td style='color:red;line-height: 0em;padding: 0em;'><xmp>></xmp></td>";
	}
	else{
		html +="<tr><td style='line-height: 0em;padding: 0em;'></td>";
	}
	$.each(data.content, function(key,val){
		$.each(val, function(key,val){
			if (key=="line"){
				if ((val==current-1)&&(thisone==false)){
					thisone = true;
				}
				else{
					thisone = false;
				}
				html+="<td style='line-height: 0em;padding: 0em;' id='"+val+"' class='"+key+"'><xmp>"+val+"</xmp></td>";
			}
			else if (key=="time"){
				html+="<td style='line-height: 0em;padding: 0em;' class='"+key+"'><xmp>"+val+"</xmp></td>";
			}
			else{
				html+="<td style='line-height: 0em;padding: 0em;' class='"+key+"'><xmp>"+val+"</xmp></td>";
			}

		});
		if (thisone==true){
			html+="</tr><tr style='color:red;background:#DCDCDC;' class='current_line' id='current_line'><td style='color:red;line-height: 0em;padding: 0em;'><xmp>></xmp></td>";
		}
		else{
			html+="</tr><tr><td style='line-height: 0em;padding: 0em;'><xmp></xmp></td>";
		}

	});
	$(id_div).html(html);
	//var page = $.mobile.activePage.attr("id");
	//if (page=="page2"){
	document.location="#current_line";
	//}
}

function completerTab(id_div,url){
	if (url =="/Server/FOP.php"){
		$.getJSON(url)
		.done(function( json ){
			completerlisteFOP(json,id_div)
		})

		.fail(function() {
			var html = "Get JSON failed";
			$(id_div).html(html);
		});
	}
	else {
		$.getJSON(url)
		.done(function( json ){
			completercurrentFOP(json,id_div);
		})
		.fail(function() {
			var html = "Get JSON failed";
			$(id_div).html(html);
		});
	}

}

function FOPcurrent(){
	var url = "/Server/currentFOP.php";
	completerTab("#tbodyInstructions",url);
}
function FOPviewer(){
	var url = "/Server/FOP.php";
	completerTab("#tbodyFOPviewer",url);
}

function pause(){
	clearInterval(refresh);
	refresh = null;
}

function play(){
	if (refresh !== null) return;
	refresh = setInterval(FOPcurrent, savedtime);
}

function refreshdata(time){
	$.post( "/Server/controlFOPPanel.php")
	.done(function( data ) {
		if (data == 1){
			$("#executor").show();
		}
	})
	.fail(function() {
		alert("controlFOPPanel failed")
	});
	
	savedtime = time;
	refresh = setInterval(FOPcurrent, savedtime);
	setInterval(FOPviewer, savedtime);
}




$(function(){
	$( "#play" ).bind( "click", function(event, ui) {
		play();
	});
	$( "#pause" ).bind( "click", function(event, ui) {
		pause();
	});
	
	$( ".ui-popup" ).bind({
		   popupafteropen: function(event, ui) {
			   pause();
		   }
	});
	$( ".ui-popup" ).bind({
		   popupafterclose: function(event, ui) {
			   play();
		   }
	});
	$( "#process" ).bind( "click", function(event, ui) {
		var line = null;
		$('#instructions-table tr.current_line').each(function() {
			line = $(this).find('td.line >xmp').html();
		});
		if (line != null){
//			/Rajouter
			var d = new Date();
			var x = document.getElementById("demo");
			var date =d.toDateString()+"-"+d.toTimeString();
			$.post( "/Server/FOPpost.php", { current: line, action: "process", time:date })
////////////////////////////
			.done(function( data ) {
				//////////////////////R
				$('.ui-popup').popup( "close" );
				//////////////////////
			})
			.fail(function() {
				alert("Process failed")
			});
		}

	});
	$( "#skip" ).bind( "click", function(event, ui) {
		var line = null;
		$('#instructions-table tr.current_line').each(function() {
			line = $(this).find('td.line >xmp').html();
		});
		if (line != null){
//			/Rajouter
			var d = new Date();
			var x = document.getElementById("demo");
			var date =d.toDateString()+"-"+d.toTimeString();
			$.post( "/Server/FOPpost.php", { current: line, action: "skip", time:date })
////////////////////////////
			.done(function( data ) {
//////////////////////R
				$('.ui-popup').popup( "close" );
				//////////////////////
			})
			.fail(function() {
				alert("Skip failed")
			});
		}

	});
	$( "#abort" ).bind( "click", function(event, ui) {
		var line = null;
		$('#instructions-table tr.current_line').each(function() {
			line = $(this).find('td.line >xmp').html();
		});
		if (line != null){
//			/Rajouter
			var d = new Date();
			var x = document.getElementById("demo");
			var date =d.toDateString()+"-"+d.toTimeString();
			$.post( "/Server/FOPpost.php", { current: line, action: "abort", time:date })
///////////////////////////////////////
			.done(function( data ) {
//////////////////////R
				$('.ui-popup').popup( "close" );
				//////////////////////
			})
			.fail(function() {
				alert("Skip failed")
			});
		}

	});

});